﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

public partial class Home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblSendPasswordMail.Visible = false;
    }

    protected void OnLoggedIn(object sender, EventArgs e)
    {
    }

    protected void LoginButton_Click(object sender, EventArgs e)
    {
        User user = new User();
        if (EventDAO.isValidUser(login.UserName.ToLower()))
        {
            user = EventDAO.login(login.UserName.ToLower(), login.Password);

            if (user != null && user.userId != 0)
            {
                Session["LoginName"] = user.loginName;
                Session["UserTypeId"] = user.userTypeId;
                Session["UserType"] = user.userType;
                Session["UserId"] = user.userId;
                Session["MobileNumber"] = user.mobileNumber;
                Session["EmailId"] = user.emailId;
                Session["level"] = user.level;
                Response.Redirect("Dashboard.aspx");
            }
            else
            {
                //lblLoginError.Text = "Invalid LoginName/Password";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "key_name", "alert('Invalid Username/Password. Please try again.');", true);
            }
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "key_name", "alert('User name does not exist.');", true);
        }
    }

    protected void btnForgotPassword_Click(object sender, EventArgs e)
    {
        User user = new User();
        user = EventDAO.forgotPass(tbForgotPasswordEmailId.Text);

        if (user != null && user.userId != 0)
        {
            this.lblForgotPasswordStatusMessage.Text = "";
            try
            {
                SendEmail(user);
                lblSendPasswordMail.Visible = true;
            }
            catch (Exception ex)
            {
                this.lblForgotPasswordStatusMessage.Text = "There is some problem with your internet connection. Please check and try again.";
                throw ex;
            }
        }
        else
        {
            this.lblForgotPasswordStatusMessage.Text = "Please enter a valid email id.";
        }
    }

    private static void SendEmail(User u)
    {
        MailMessage mailMessage = new MailMessage();
        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com",587);
        smtpClient.EnableSsl = true;
        smtpClient.Credentials = new System.Net.NetworkCredential("organizerwork@gmail.com", "Rizwan@123"); // User id and password.
        mailMessage.From = new MailAddress("organizerwork@gmail.com");
        mailMessage.To.Add(new MailAddress("organizerwork@gmail.com"));
        mailMessage.To.Add(new MailAddress(u.emailId));
        mailMessage.ReplyTo = new MailAddress("organizerwork@gmail.com");
        mailMessage.Subject = "Your account credentials";
        mailMessage.Body = "Dear " + u.userName + " \n\n Your account credentials are \n\n login name: " + u.userName + "\n password : " + u.password + "\n\nRegards \n admin.";
        smtpClient.Send(mailMessage);
    }
}
